package com.weblog.controller.user;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.code.kaptcha.Constants;
import com.weblog.model.user.User;
import com.weblog.msg.UserOperationEnum;
import com.weblog.pojo.user.UserLogin;
import com.weblog.service.UserService;
import com.weblog.utils.ObjectUtils;

@Controller
@RequestMapping(value = "/user")
public class UserController {

	private final static Log log = LogFactory.getLog(UserController.class);

	@Autowired
	private UserService userService;

	@Autowired
	private HttpServletRequest request;

	/**
	 * @author zhangHaiWen
	 * @date 2018/7/8 14:23
	 * @desc 查找用户使用的方法
	 * @param *
	 * @param null
	 * @return
	 */
	@RequestMapping(value = "/find")
	@ResponseBody
	public Object findUser() {
		User user = userService.getUserById(1);
		log.info("findUser:{" + user + "}" + "_{" + user + "}");
		return user;
	}

	/**
	 * @author zhangHaiWen
	 * @date 2018/7/8 14:28
	 * @desc 登录页面跳转
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/login")
	public String useLogin(Model model) {
		return "/user/userlogin";
	}

	/**
	 * @author zhangHaiWen
	 * @date 2018/7/8 14:28
	 * @desc 用户登录的方法
	 * @param *
	 * @param userLogin
	 *            用户登录的一些信息
	 * @return
	 */
	@RequestMapping(value = "/do/login", method = RequestMethod.POST)
	public String userDoLogin(UserLogin userLogin, Model model, RedirectAttributes redirectAttributes) {
		log.info("userlogin:{" + userLogin + "}");
		String kaptchaExpected = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
		// 校验验证码是否正确
		if (userLogin.getKaptcha() == null || !kaptchaExpected.equals(userLogin.getKaptcha())) {
			model.addAttribute("msg", UserOperationEnum.U002.getMsg());
			return "/user/userlogin";
		}
		User user = userService.doLogin(userLogin);
		if (ObjectUtils.ObjectIsNotNull(user)) {
			//TODO 这里这样写在拎一个Controller中获取不到
			model.addAttribute("user", user);
			return "forward:/weblog/index";
		} else {
			// 返回账号密码不正确
			model.addAttribute("msg", UserOperationEnum.U001.getMsg());
			return "/user/userlogin";
		}
	}

	/**
	 * 用户注册页面跳转
	 * 
	 * @param userPwdConfirm
	 * @return
	 */
	@RequestMapping(value = "/register", method = { RequestMethod.POST, RequestMethod.GET })
	public String userRegister(Model model) {
		return "/user/userregister";
	}

	/**
	 * @author zhangHaiWen
	 * @date 2018/7/8 15:40
	 * @desc 用户注册跳转方法
	 * @param *
	 * @param null
	 * @return
	 */
	@RequestMapping(value = "/do/register", method = { RequestMethod.POST })
	public String userDoRegister(User user, String userPwdConfirm, Model model, RedirectAttributes redirectAttributes) {
		log.info("userRegiest:{" + user + "}" + "_{" + userPwdConfirm + "}");
		// 获取ip地址
		String IP = request.getRemoteAddr();
		// 用户上次登录IP
		user.setUserLastLoginIp(IP);
		// 用户注册IP
		user.setUserRegisterIp(IP);
		if (ObjectUtils.ObjectIsNotNull(user)) {
			int result = userService.doRegiest(user, userPwdConfirm);
			if (result > 0) {
				model.addAttribute("user", user);
				return "forward:/weblog/index";
			}
		}
		return "/user/userRegister";
	}
}
